package com.itboye.sunsun.person.ui;

import java.net.URLDecoder;
import java.net.URLEncoder;

import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.OrderBean;
import com.itboye.sunsun.beans.OrderQueRenDingDan;
import com.itboye.sunsun.buy.ui.QueRenZhiFuActivity;
import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.utils.Sha1Md5Util;
import com.umeng.message.proguard.A;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;
import u.aly.ao;

public class PointMarketActivity extends BaseActivity implements OnClickListener {

	private ImageView back;
	private WebView webview;
	private WebView choujiangwebview;
	String baseurl = NetPublicConstant.secondUrl+"/Webview";
	

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_pointmarket);
		
		webviewsettings();
//		choujiangwebviewsettings();
	}

//	private void choujiangwebviewsettings() {
//		// TODO Auto-generated method stub
//		final String uid = "uid" + "=" + (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, "");
//		final String password = "psw" + "=" + (String) SPUtils.get(App.ctx, null, SPContants.PASSWORD, "");
//
//		String url = baseurl+ "/Game/index?" + uid + "&" + password;
//		choujiangwebview.getSettings().setJavaScriptEnabled(true);
//		choujiangwebview.loadUrl(url);
//		// webview.postUrl(url, postData);
//		// 第一个
//		choujiangwebview.setWebViewClient(new WebViewClient()
//
//		{
//
//			@Override
//			public boolean shouldOverrideUrlLoading(WebView view, String url) {
//
//				// Toast.makeText(App.ctx, url, 0).show();
//
//				return true;
//			}
//
//			// @Override
//			// public void onReceivedError(WebView view, int errorCode, String
//			// description, String failingUrl) {
//			// // TODO Auto-generated method stub
//			// // super.onReceivedError(view, errorCode, description,
//			// // failingUrl);
//			// view.loadUrl("file:///android_asset/nonet.html");
//			// }
//
//		});
//
//	}

	public void webviewsettings() {

		final String uid = "uid" + "=" + (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, "");
		final String password = "psw" + "=" + (String) SPUtils.get(App.ctx, null, SPContants.PASSWORD, "");

		String url = baseurl + "/ScoreGoods/index?" + uid + "&" + password;
		webview.getSettings().setJavaScriptEnabled(true);
		webview.loadUrl(url);
		// webview.postUrl(url, postData);
		// 第一个
		webview.setWebViewClient(new WebViewClient()

		{

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {

				// Toast.makeText(App.ctx, url, 0).show();

				if (url.contains("SUNSUN_ALIPAY_FOR_SCOREGOODS")) {
					String a[] = url.split("&code=");
					String b[] = a[1].split("&name=");
					String code = b[0];
					String c[] = b[1].split("&desc=");
					String name = c[0];
					String d[] = c[1].split("&total_price=");
					String desc = d[0];
					String e[] = d[1].split("&key=");
					String totalPrice = e[0];
					String f[] = e[1].split("&show_url");
					String key = f[0];
					String showUrl = f[1];

					// 需要校验
					name = URLDecoder.decode(name);
					desc = URLDecoder.decode(desc);
					String str = code + name + desc + totalPrice;

					str = URLEncoder.encode(str);

					str = str.toLowerCase();

					str = Sha1Md5Util.SHA1(Sha1Md5Util.MD5(str));

					str = str + "ITBOYE_PAY_DASFSAFKL";

					str = Sha1Md5Util.MD5(str);

					if ((key.equals(str))) {
						OrderQueRenDingDan order = new OrderQueRenDingDan();
						order.setCode(code);
						order.setDesc(desc);
						order.setKey(key);
						order.setName(name);
						order.setShowUrl(showUrl);
						order.setTotalPrice(totalPrice);
						Intent intent = new Intent(PointMarketActivity.this, QueRenZhiFuActivity.class);
						intent.putExtra("what", order);
						startActivity(intent);

					}

				} else {
					view.loadUrl(url);
				}

				return true;
			}

			// @Override
			// public void onReceivedError(WebView view, int errorCode, String
			// description, String failingUrl) {
			// // TODO Auto-generated method stub
			// // super.onReceivedError(view, errorCode, description,
			// // failingUrl);
			// view.loadUrl("file:///android_asset/nonet.html");
			// }

		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			this.finish();
			break;

		default:
			break;
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (webview.canGoBack()) {
				webview.goBack();
				return true;
			}

		}
		return super.onKeyDown(keyCode, event);
	}

}
